#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#-*- coding: utf-8 -*-

import numpy as np
import parl
from parl import layers
from paddle import fluid


class Agent(parl.Agent):
    def __init__(self, algorithm, obs_dim, act_dim):
        assert isinstance(obs_dim, list)
        assert isinstance(act_dim, int)
        self.obs_dim = obs_dim
        self.act_dim = act_dim
        super(Agent, self).__init__(algorithm)
        # 注意：最开始先同步self.model和self.target_model的参数.
        self.alg.sync_target(decay=0.0)

    def build_program(self):
        self.pred_program = fluid.Program()
        self.learn_program = fluid.Program()

        with fluid.program_guard(self.pred_program):
            obs = layers.data(
                name='obs', shape=[None, self.obs_dim[2], self.obs_dim[0], self.obs_dim[1]], dtype='float32')
            self.pred_act = self.alg.predict(obs)

        with fluid.program_guard(self.learn_program):
            obs = layers.data(
                name='obs', shape=[None, self.obs_dim[2], self.obs_dim[0], self.obs_dim[1]], dtype='float32')
            act = layers.data(name='act', shape=[1], dtype='int32')
            reward = layers.data(name='reward', shape=[], dtype='float32')
            terminal = layers.data(name='terminal', shape=[], dtype='bool')
            # act = layers.data(name='act', shape=[None, 1], dtype='int32')
            # reward = layers.data(name='reward', shape=[None, 1], dtype='float32')
            # terminal = layers.data(name='terminal', shape=[None, 1], dtype='bool')
            next_obs = layers.data(
                name='next_obs', shape=[None, self.obs_dim[2], self.obs_dim[0], self.obs_dim[1]], dtype='float32')


            _, self.critic_cost = self.alg.learn(obs, act, reward, next_obs, terminal)

    def sample(self, obs):
        # obs = np.expand_dims(obs, axis=0)
        act_prob = self.fluid_executor.run(
            self.pred_program,
            feed={'obs': obs.astype('float32')},
            fetch_list=[self.pred_act])[0]
        act_prob = np.squeeze(act_prob, axis=0)
        act = np.random.choice(range(self.act_dim), p=act_prob)
        return act

    def predict(self, obs):
        obs = np.expand_dims(obs, axis=0)
        act_prob = self.fluid_executor.run(
            self.pred_program, feed={'obs': obs.astype('float32')},
            fetch_list=[self.pred_act])[0]
        # act = np.squeeze(act)
        act_prob = np.squeeze(act_prob, axis=0)
        act = np.argmax(act_prob)  # 根据动作概率选择概率最高的动作
        return act

    def learn(self, obs, act, reward, next_obs, terminal):
        feed = {
            'obs': obs.astype('float32'),
            'act': act.astype('int32'),
            'reward': reward,
            'next_obs': next_obs.astype('float32'),
            'terminal': terminal
        }
        critic_cost = self.fluid_executor.run(
            self.learn_program, feed=feed, fetch_list=[self.critic_cost])[0]
        self.alg.sync_target()
        return critic_cost
